//
//  JYPicCollectionView.swift
//  WeiBoDemo
//
//  Created by 万佳阳 on 16/8/4.
//  Copyright © 2016年 JYWan. All rights reserved.
//

import UIKit
import SDWebImage

class JYPicCollectionView: UICollectionView {
    //定义一个属性
    var picURLs : [NSURL] = [NSURL]() {
        didSet {
            reloadData()
        }
    }

    override func awakeFromNib() {
        super.awakeFromNib()
        dataSource = self
        delegate = self
    }
}


// MARK:- 数据源代理
extension JYPicCollectionView : UICollectionViewDataSource, UICollectionViewDelegate {
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return picURLs.count
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier("picCell", forIndexPath: indexPath) as! JYPicCollectionViewCell
        
        cell.picURL = picURLs[indexPath.item]
    
        return cell
    }
    
    func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
        //设置通知需要传递的参数
        let userInfo : [String : AnyObject] = [showPhotoBrowserIndexKey : indexPath, showPhotoBrowserPicURLsKey : picURLs]
        
        //发出通知
        NSNotificationCenter.defaultCenter().postNotificationName(showPhotoBrowserNote, object: self, userInfo: userInfo)
    }
}


// MARK:- AnimationPresentedDelegate
extension JYPicCollectionView : AnimationPresentedDelegate {
    func startRect(indexPath: NSIndexPath) -> CGRect {
        //获取cell
        let cell = cellForItemAtIndexPath(indexPath)!
        
        //获取cell的frame
        let startFrame = convertRect(cell.frame, toCoordinateSpace: UIApplication.sharedApplication().keyWindow!)
        return startFrame
    }
    
    func endRect(indexPath: NSIndexPath) -> CGRect {
        //获取该位置的URL对象
        let picURL = picURLs[indexPath.item]
        let image = SDWebImageManager.sharedManager().imageCache.imageFromDiskCacheForKey(picURL.absoluteString)
        let width = UIScreen.mainScreen().bounds.width
        let height = width / image.size.width * image.size.height
        var y : CGFloat = 0
        if height < UIScreen.mainScreen().bounds.height {
            y = (UIScreen.mainScreen().bounds.height - height) * 0.5
        }
        return CGRect(x: 0, y: y, width: width, height: height)
    }
    
    func imageView(indexPath: NSIndexPath) -> UIImageView {
        let imageView = UIImageView()
        
        //获取该位置的URL对象
        let picURL = picURLs[indexPath.item]
        let image = SDWebImageManager.sharedManager().imageCache.imageFromDiskCacheForKey(picURL.absoluteString)
        imageView.image = image
        imageView.contentMode = .ScaleAspectFill
        imageView.clipsToBounds = true
        
        return imageView
    }
}

class JYPicCollectionViewCell: UICollectionViewCell {
    //定义一个模型属性
    var picURL : NSURL? {
        didSet {
            guard let picURL = picURL else {
                return
            }
            iconView.sd_setImageWithURL(picURL, placeholderImage: UIImage(named: "empty_picture"))
            
        }
    }
    
    
    // MARK:- 控件属性
    @IBOutlet weak var iconView: UIImageView!
}